package drrr.orm;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import java.util.List;

/**
 * Created by lirance on 15/5/19.
 */
public class RoomChatListDao extends HibernateDaoSupport {

    private static final Log log = LogFactory.getLog(RoomChatListDao.class);

    /**
     * delete a existed roomChatListEntity
     *
     * @param roomChatListEntity a existed roomEntity
     */
    public void delete(RoomChatListEntity roomChatListEntity) {
        Session session = getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();
        session.delete(roomChatListEntity);
        transaction.commit();
        session.close();
    }

    /**
     * insrt a new  RoomMemberEntity to the database
     *
     * @param roomChatListEntity
     */
    public void insert(RoomChatListEntity roomChatListEntity) {
        Session session = getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();
        session.save(roomChatListEntity);
        transaction.commit();
        session.close();
    }

    /**
     * update the roomChatList
     *
     * @param roomChatListEntity the modified RoomEntity
     */
    public void update(RoomChatListEntity roomChatListEntity) {
        Session session = getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();
        session.update(roomChatListEntity);
        transaction.commit();
        session.close();
    }

    /**
     * get all of the roomChatLists
     *
     * @return the list of roomChatLists
     */
    public List getAllRoomChatLists() {
        Session session = getSessionFactory().openSession();
        List list = null;
        try {
            list = session.createQuery("from RoomChatListEntity ").list();
        } catch (Exception e) {
            list = session.createQuery("from RoomChatListEntity ").list();
        }
        session.close();
        return list;
    }

    public List<Entity> getRoomChatListbyId(int roomId) {
        Session session = getSessionFactory().openSession();
        List list;
        try {
            list = session.createQuery("from RoomChatListEntity where roomid=" + roomId).list();
        } catch (Exception e) {
            list = session.createQuery("from RoomChatListEntity where roomid=" + roomId).list();
        }
        session.close();
        return list;

    }

}
